const http = require('http');
const https = require('https');
const fs = require('fs');
const path = require('path');
const Koa = require('koa');
const Static = require('koa-static');
const cors = require('@koa/cors');

/**
 * https
 */
const app1 = new Koa();
app1.use(Static(path.resolve(__dirname, 'static')))
const httpsOptios = {
    key: fs.readFileSync("./server.key", "utf8"),
    cert: fs.readFileSync("./server.cert", "utf8")
};
https.createServer(httpsOptios, app1.callback())
    .listen(3001, 'localhost', listeningReporter)

/**
 * http
 */
const app2 = new Koa();
app2.use(cors())
http.createServer(app2.callback())
    .listen(3000, 'localhost', listeningReporter)
app2.use(async ctx => {
    ctx.body = 'wcx'
})

function listeningReporter() {
    const { address, port } = this.address();
    const protocol = this.addContext ? 'https' : 'http';
    console.log(`Listening on ${protocol}://${address}:${port}...`);
}